
#include "timer1.h"
int delayLoop=0;

//timer controls pin reading of binary sensors

void initTimer1(void)
{
	gtimer1Flags = 0;
	pollFreq = 10000;
	buffercnt = 0;

	ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1);
	//set timer period: 40MHz/10/2 = 2MHz = 0.5us
	//for 1000us = 1ms use Div by 0.1 and then 4, 40MHz / 0.1 / 4 = 1ms
	ROM_TimerConfigure(TIMER1_BASE, TIMER_CFG_PERIODIC);
	ulPeriod = (ROM_SysCtlClockGet() / pollFreq);
	ROM_TimerLoadSet(TIMER1_BASE, TIMER_A, ulPeriod - 1);

	//Timer interrupt setup
	ROM_TimerIntClear(TIMER1_BASE, TIMER_TIMA_TIMEOUT);
	ROM_TimerIntEnable(TIMER1_BASE, TIMER_TIMA_TIMEOUT);
	ROM_IntEnable(INT_TIMER1A);
	ROM_TimerEnable(TIMER1_BASE, TIMER_A);
	bt_UARTsend("Init Timer1");
	loading_bar(24);
	load_okfail(1);


	memset(gOutput, 0, sizeof(gOutput));
	//memset(gHistory, 0, sizeof(gHistory));
	memset(gStateChange, 0, sizeof(gStateChange));
}

void timer1Handler(void)
{
	int iterator=0;
	//reset counter value back to ulPeriod
	ROM_TimerLoadSet(TIMER1_BASE, TIMER_A, ulPeriod-1);
	ROM_TimerIntClear(TIMER1_BASE, TIMER_TIMA_TIMEOUT);

	buffer[0] += (ROM_GPIOPinRead(GPIO_PORTB_BASE, _beam1)) ? 1 : -1;
	buffer[1] += (ROM_GPIOPinRead(GPIO_PORTB_BASE, _beam2)) ? 1 : -1;
	buffer[2] += (ROM_GPIOPinRead(GPIO_PORTB_BASE, _beam3)) ? 1 : -1;
	buffer[3] += (ROM_GPIOPinRead(GPIO_PORTA_BASE, _hall1)) ? 1 : -1;
	buffer[4] += (ROM_GPIOPinRead(GPIO_PORTE_BASE, _hall2)) ? 1 : -1;

	for (iterator = 0 ; iterator < 5 ; iterator++ )
	{
		gHistory[iterator] = gOutput[iterator];
		if (buffer[iterator] > 11)
		{
			gOutput[iterator]=1;
			if (buffer[iterator] > 15) { buffer[iterator] = 15; }	//max range of input samples
		}
		if (buffer[iterator] < 4)
		{
			gOutput[iterator]=0;
			if (buffer[iterator] < 0)  { buffer[iterator] = 0; }	//min range
		}

		if(gOutput[iterator] != gHistory[iterator])
		{
			gStateChange[iterator] = 1;
		}
	}
	delayLoop++;
	//clear interrupt
	//ROM_TimerIntClear(TIMER1_BASE, TIMER_TIMA_TIMEOUT);
	if(calibrateState == 0 || calibrateState == 1)
	{
		if(gStateChange[0])//GATEWAY BEAMBREAK
		//	if(1)
		{
			if(gOutput[0] == 0)		//Entry
			{
				if (_gDrop == 0 && _gSC==0)
				{
					belt_driver(0);

				}
				else
				{
					belt_driver(1);
					gStateChange[0] = 0;
					//ROM_SysCtlDelay(1000000);
				}
				//delayLoop=0;
			}
			else				//Exit
			{
				gDC=1;			//Set Drop Complete
				gStateChange[0] = 0;
				_gDrop=0;
				//delayLoop=0;
			}
		}
	}
}

//Below is example of hysteresis - image is not perfectly to scale
/*					___			____
 * 20			   /   \  /^^^\/    \
 * 15			  /:	\/			 \
 * 10			 / :				  \
 * 5			/  :				   \
 * 0		___/   :				   :\_____/\__/v\__
 * (t)->   00-05-10-15-20-25-30-35-40-45-50-55-60-65-70-75...
 * 				   :				   :
 * 				   :___________________:
 * 1			   |				   |
 * -			   |	output		   |
 * -			   |				   |
 * 0		_______|				   |__________________
 * (t)->   00-05-10-15-20-25-30-35-40-45-50-55-60-65-70-75...
 *
 *	  _____	   ______  __   __  ______  ______  ______
 *	 /\  __`-./\  __ \ \ \ /\ \ \  ___\/\  ___\ \  __ \
 *	/  \ \ `\ \ \ \_\ \ \ `-.\ \ \ \ ___ \ \__   \ \_\ \
 *  \   \ \  \ \ \  _  \ \      \ \ \\_ \ \  _\   \  __`.
 *	 \	 \ \_/ |  \ \ \ \ \ \`-. \ \ \_\ \ \ \____ \ \ \ \
 *	  \	  \____/   \ \ \_\ \_\  \_\ \_____\ \_____\ \_\ \ \
 *	   \  /   / \  / / / / / /  / / /     / /     / / / / /
 *		\/___/	 \/_/\/_/ /_/ \/_/ /_____/ /_____/ /_/\/_/
 *
 *							DANGER?
 *
 */



